/*
	vars:
		hide_available: removes the "available tags" link (and structure)
		suggest_min_len: minimum number of chars before the suggest box opens up
*/
WWL.tags = function ( name, vars )
{
	var tags = new WWL ( 'tags', name );

	if ( ! vars ) vars = {};

	tags.events = {
		"update" : null,
		"list-tags" : null
	};

	tags._hide_available = vars.get ( "hide_available", 0 );
	tags._suggest_min_len = vars.get ( "suggest_min_len", 3 );

	tags.form = new liwe.form.instance ( tags.id );
	tags.form.text   ( { label: "", name: "_tags", value: "", size: vars.get ( "size", 20 ),
		maxlength: 250, onblur: "WWL.tags._add_tags('" + tags.name + "')",
		filter: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz 0123456789_-,"
		} );
	tags.form.workspace ( { name: tags.id + "@tags_added" } );
	if ( ! tags._hide_available ) tags.form.workspace ( { name: tags.id + "@tags_available" } );

	tags.form._data = {};
	tags._available_hidden = true;

	tags.to_string = function ()
	{
		return tags.form.get ();
	};

	tags.get_value = function ()
	{
		var k = tags.form._data.keys ();

		k.sort ();
		return '|'.join ( k );
	};

	tags.set_value = function ( v )
	{
		tags.form._data = {};
		WWL.tags._parse_txt ( tags, v, "|" );
	};

	tags._refresh = function ()
	{
		var tag_widget = tags.form.get_element ( "_tags" );
		WWL.tags._suggest_init ( tag_widget.id, tags );
	};

	return tags;
};

WWL.tags._suggest_init = function ( dest_id, tags_obj )
{ 
	var names = null;

	tags.list ( tags_obj.module_name, function ( v ) { tags_obj._curr_tags_list = v; }, true );

	function request ( txt, cback )
	{
		var lst = [], t, l, tag;

		txt = txt.split ( "," );
		txt = txt [ txt.length -1 ].strip();

		if ( txt.length < tags_obj._suggest_min_len ) 
		{
			as.hide ();
			return;
		}

		if ( ! names )
 			names = WWL.tags._mk_name_list ( tags_obj, ( tags_obj._curr_tags_list ? tags_obj._curr_tags_list : {} ) );

		l = names.length;

		if ( ! l ) 
		{
			as.hide ();
			return;
		}

		for ( t = 0; t < l; t ++ )
		{
			tag = names [ t ];
			if ( tag.startsWith ( txt ) ) lst.push ( [ tag, tag ] );
		}

		cback ( lst );
	}

	function row_render ( row, is_selected )
	{
		return '<div class="value">' + row [ 1 ] + '</div>';
	}

	function row_get_val ( row )
	{
		return row [ 0 ];
	}

	var as = new AutoSuggest ( dest_id );

	as.cbacks [ 'request' ] = request;
	as.cbacks [ 'row-render' ] = row_render;
	as.cbacks [ 'row-get-val' ] = row_get_val;
};

WWL.tags.templates = {
		"tag-item" : '<div id="tag-del-cnt-%(name)s" class="tag-del-cnt"><div class="tag-del" onmouseover="this.className=\'tag-del-hover\'" onmouseout="this.className=\'tag-del\'" onclick="WWL.tags._del_tag(\'%(tags_name)s\',\'%(name)s\')"></div>%(name)s</div>',
		"tag-available" : '<div class="tag-available"><div class="action"><a href="javascript:WWL.tags.available_toggle(\'%(tags_name)s\')">Tags disponibili</a></div>' +
				  '<div class="tag-available-list" id="%(tags_id)s-cnt"></div></div>',
		"wait-medium" : '<div class="liwe_progress" align="center"><div class="medium_white"></div></div>',
		"tag-add" : '<a class="tag-add" href="javascript:WWL.tags._add_tag(\'%(tags_name)s\',\'%(name)s\')">%(name)s</a> ',
		"tag-update" : '<div class="action"><a class="tag-add" href="javascript:WWL.tags._update(\'%(tags_name)s\')">Aggiorna lista tags</a></div>'
	};


WWL.tags.get_instance = function ( instance_name )
{
	return WWL.get_instance ( "tags", instance_name );
};

WWL.tags._add_tags = function ( name )
{
	var tags = WWL.tags.get_instance ( name );
	var txt = tags.form.get_value ( "_tags" );

	WWL.tags._parse_txt ( tags, txt, "," );
	tags.form.set_value ( "_tags", "" );
};

WWL.tags.available_toggle = function ( name )
{
	var tags = WWL.tags.get_instance ( name );

	if ( tags._available_hidden == true )
	{
		var e = $( tags.id + "-cnt" );
		e.innerHTML = WWL.tags.templates [ 'wait-medium' ];

		e.style.display = "block";
		tags._available_hidden = false;

		if ( tags.events [ 'list-tags' ] )
		{
			tags.events [ 'list-tags' ] ( function ( v ) { WWL.tags._show_avail ( tags, v ); } );
		}
	} else {
		$( tags.id + "-cnt" ).style.display = "none";
		tags._available_hidden = true;
	}
};

WWL.tags._mk_name_list = function ( tags, all_tags_list )
{
	var names = all_tags_list.keys ();
	var t, l, name;
	var lst = [];

	names.sort ();
	l = names.length;

	for ( t = 0; t < l; t ++ )
	{
		name = names [ t ];
		if ( tags.form._data [ name ] ) continue;

		lst.push ( name );
	}

	return lst;
};

WWL.tags._show_avail = function ( tags, all_tags_list )
{
	if ( ( ! all_tags_list ) || ( tags._hide_available ) ) return;

	var names = WWL.tags._mk_name_list ( tags, all_tags_list );
	var t, l, name;
	var s = new String.buffer ();

	l = names.length;

	for ( t = 0; t < l; t ++ )
	{
		name = names [ t ];

		s.add ( String.formatDict ( WWL.tags.templates [ 'tag-add' ], { "tags_name" : tags.name, "name" : name } ) );
	}

	tags._curr_tags_list = all_tags_list;

	s = s.toString ();

	if ( ! s.length ) s = "Non ci sono altri tag disponibili";

	$( tags.id + "-cnt", s + String.formatDict ( WWL.tags.templates [ 'tag-update' ], { "tags_name" : tags.name } ) );
};


WWL.tags._parse_txt = function ( tags, txt, sep )
{
	if ( ! $( tags.id + "-cnt" ) )
	{
		if ( ! tags._hide_available )
			$( tags.id + "@tags_available", String.formatDict ( WWL.tags.templates [ 'tag-available' ], { "tags_id" : tags.id, "tags_name" : tags.name } ) );
	}

	if ( ! txt  || txt.length == 0 ) txt = "";

	var tags_form = tags.form;
	var lst = txt.split ( sep );
	var t, l = lst.length;
	var k;

	for ( t = 0; t < l; t ++ )
	{
		k = lst [ t ].strip ().toLowerCase ();
		if ( ! k.length ) continue;
		tags_form._data [ k ] = 1;
	}

	WWL.tags._show_tags ( tags, tags_form );
};

WWL.tags._show_tags = function ( tags, tags_form )
{
	var t, l;
	var k;

	k = tags_form._data.keys ();
	k.sort ();

	var res = new String.buffer ();

	l = k.length;
	for ( t = 0; t < l; t ++ )
		res.add ( String.formatDict ( WWL.tags.templates [ 'tag-item' ], { "name" : k [ t ], "tags_name" : tags.name } ) );

	$( tags.id + "@tags_added", res.toString () );
	tags_form.set_value ( "_tags", "" );

	WWL.tags._show_avail ( tags, tags._curr_tags_list );

	if ( tags.events [ 'update' ] ) tags.events [ 'update' ] ( tags.get_value () );
};

WWL.tags._del_tag = function ( tags_name, name )
{
	var tags = WWL.tags.get_instance ( tags_name );
	var tags_form = tags.form;
	
	$( "tag-del-cnt-" + name ).style.display = "none";

	delete tags_form._data [ name ];
	WWL.tags._show_avail ( tags, tags._curr_tags_list );
	if ( tags.events [ 'update' ] ) tags.events [ 'update' ] ( tags.get_value () );
};

WWL.tags._add_tag = function ( tags_name, name )
{
	var tags = WWL.tags.get_instance ( tags_name );
	var tags_form = tags.form;
	
	tags_form._data [ name ] = 1;

	// WWL.tags._show_avail ( tags, tags._curr_tags_list );
	WWL.tags._show_tags ( tags, tags_form );
};

WWL.tags._update = function ( tags_name )
{
	var tags = WWL.tags.get_instance ( tags_name );
	
	if ( tags.events [ 'list-tags' ] )
	{
		var e = $( tags.id + "-cnt" );
		e.innerHTML = WWL.tags.templates [ 'wait-medium' ];
		tags.events [ 'list-tags' ] ( function ( v ) { WWL.tags._show_avail ( tags, v ); }, true );
	}
};
